﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace C.Unite
{
    public class CacheHelper
    {
        private static Cache _cache;
        public CacheHelper()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }

        #region 方法

        //实现“一键一值”存储方法,最普通的存储方法   
        //（“一键一值”指一个Identify存储一个值,下面还有一个“一键多值”方法，因为有时候需要一个键存储多个变量对象值） 
        /// <summary>
        /// 实现“一键一值”存储方法,最普通的存储方法  
        /// </summary>
        /// <param name="strIdentify">主键名</param>
        /// <param name="Info">对象值</param>
        /// <param name="timsec">时间</param>
        /// <param name="timetype">时间类型：0:秒，1:小时，2:天</param>
        /// <returns></returns>
        public static bool InsertIdentify(string strIdentify, object Info, int timsec, int timetype)
        {
            DateTime dt = DateTime.Now;
            if (timetype == 0)
            {
                dt = dt.AddSeconds(timsec);
            }
            if (timetype == 1)
            {
                dt = dt.AddHours(timsec);
            }
            if (timetype == 2)
            {
                dt = dt.AddDays(timsec);
            }
            if (strIdentify != null && strIdentify.Length != 0 && Info != null)
            {
                //建立回调委托的一个实例 
                CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
                HttpContext.Current.Cache.Insert(strIdentify, Info, null, dt,
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Default,
                callBack);
                return true;
            }
            else
            {
                return false;
            }
        }

        //判断存储的"一键一值"值是否还存在（有没有过期失效或从来都未存储过） 
        public static bool ExistIdentify(string strIdentify)
        {
            try
            {
                if (HttpContext.Current.Cache[strIdentify] != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception et)
            {
                return false;
                throw;
            }

        }



        //读取“一键多值”Identify的值 
        public static object ReadIdentify(string strIdentify)
        {
            //取出值 
            return (object)HttpContext.Current.Cache[strIdentify];
        }

        //手动移除“一键一值”对应的值 
        public static bool RemoveIdentify(string strIdentify)
        {
            //取出值 
            if ((object)HttpContext.Current.Cache[strIdentify] != null)
            {
                HttpContext.Current.Cache.Remove(strIdentify);
            }
            return true;
        }


        //此方法在值失效之前调用，可以用于在失效之前更新数据库，或从数据库重新获取数据 
        private static void onRemove(string strIdentify, object userInfo, CacheItemRemovedReason reason)
        {
        }

        #endregion
    }
}